package com.hualan.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hualan.mapper.FeedbackMapper;
import com.hualan.pojo.PageResult;
import com.hualan.pojo.Questions;
import com.hualan.service.FeedbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.hualan.pojo.Feedback;

import java.util.List;

@Service
public class FeedbackServiceImpl implements FeedbackService {

    @Autowired
    private FeedbackMapper feedbackMapper;

    /**
     * 根据 quesId 查询反馈信息及关联的顾问姓名（只读事务）
     */

    @Override
    public PageResult<Feedback> getAllFeedback(Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Feedback> feedbackList = feedbackMapper.selectAllFeedback();
        Page<Feedback> p = (Page<Feedback>) feedbackList;
        return new PageResult<>(p.getTotal(), p.getResult());
    }

    @Override
    @Transactional(readOnly = true)
    public PageInfo<Feedback> getFeedbackWithConsultantByQuesId(Integer quesId, int pageNum, int pageSize) {
        // 使用PageHelper进行分页
        PageHelper.startPage(pageNum, pageSize);
        List<Feedback> feedbackList = feedbackMapper.selectFeedbackWithConsultantByQuesId(quesId);
        return new PageInfo<>(feedbackList);
    }

    @Override
    @Transactional
    public int deleteByPrimaryKey(Long id) {
        return feedbackMapper.deleteByPrimaryKey(id);
    }

    //批量删除问题
    @Override
    @Transactional
    public int batchDelete(List<Long> ids) {
        return feedbackMapper.batchDelete(ids);
    }

    //插入问题
    @Override
    @Transactional
    public int addFeedback(Feedback feedback) {
        return feedbackMapper.insert(feedback);
    }

    @Override
    public PageResult<Feedback> searchByContent(String content, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Feedback> feedbackList = feedbackMapper.searchByContent(content);
        Page<Feedback> p = (Page<Feedback>) feedbackList;
        return new PageResult<>(p.getTotal(), p.getResult());
    }
}
